package com.lxq.live.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.lxq.live.pojo.VideoRow;
import com.lxq.live.pojo.VideoStompBody;
import com.lxq.live.service.AudienceService;
import com.lxq.live.service.VideoService;
import com.lxq.live.util.VideoBarrageRepository;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Random;


@RestController
@Api(tags = "视频相关接口接口")
public class VideoController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Autowired
    private VideoBarrageRepository barrage;
    @Autowired
    AudienceService audienceService;
    @Autowired
    VideoService videoService;

    @MessageMapping("/barrage")
    public String pushBarrage(VideoStompBody stompBody) throws JsonProcessingException {
        var videoUuid = stompBody.uuid;
        if (stompBody.flag) {
            audienceService.operation(videoUuid, false);
            return "[]";
        }
        var objectMapper = new ObjectMapper();
        var jsonNode = objectMapper.readTree(barrage.barrageCenter(videoUuid));
        var objectNode = objectMapper.createObjectNode();
        objectNode.put("audiences", audienceService.getAudiences(videoUuid));
        ArrayNode arrayNode = objectNode.putArray("barrages");
        if (!stompBody.playing) {
            simpMessagingTemplate.convertAndSend("/video_broker/" + stompBody.randomId + "/" + videoUuid,
                    objectNode.toString());
            return "";
        }
        var seek = stompBody.seek;
        var size = jsonNode.path(seek).size();
        if (size > 0) {
            jsonNode.path(seek).forEach(jsonNode1 -> arrayNode.addObject().set("comment", jsonNode1.findValue("comment")));
        }
        simpMessagingTemplate.convertAndSend("/video_broker/" + stompBody.randomId + "/" + videoUuid,
                objectNode.toString());
        return objectNode.toString();
    }

    @GetMapping("/random_id/{videoUuid}")
    public String randomId(@PathVariable String videoUuid) {
        audienceService.operation(videoUuid, true);
        return "{\"randomId\":\"" + new Random().nextInt(9999) + "\"}";
    }

    @GetMapping("/get_video_url/{videoUuid}")
    public String getVideoUrl(@PathVariable String videoUuid) {
        return videoService.getVideoUrl(videoUuid);
    }

    @PostMapping("/blobtest")
    public void blobVideoTest(HttpServletResponse response) throws IOException {
        var videoFile = new File("D:/live_repository/public/澜/澜.mp4");
        response.getHeaderNames().forEach(name -> {
            System.out.println("name=" + name);
        });
        response.setContentType("video/mpeg4");
        var bufferInput = new BufferedInputStream(new FileInputStream(videoFile));
        byte[] buffer = new byte[bufferInput.available()];
        bufferInput.read(buffer);
        response.reset();
        response.addHeader("Content-Length", "" + videoFile.length());
        response.getOutputStream().write(buffer);
    }

    @GetMapping("/video_row")
    public List<VideoRow> getBaseVideo() {
        return videoService.getBaseVideo();
    }
}